package dev.android.yeqh.alarmclock.common;

import java.util.Calendar;
import java.util.List;

import dev.android.yeqh.alarmclock.AlarmListActivity;
import dev.android.yeqh.alarmclock.R;
import android.app.AlarmManager;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;
import dev.android.yeqh.alarmclock.AlarmReceiver;
import dev.android.yeqh.alarmclock.db.DBM;
import dev.android.yeqh.alarmclock.model.Alarm;
import dev.android.yeqh.alarmclock.model.AlarmType;

public class utils {
	public static void newAlarm(Context context, long alarmTime, AlarmType alarmType, long alarmInterval, String alarmDescription, long alarmId) {
		if (alarmId == -1) {
			alarmId = new DBM(context).insert(alarmTime, alarmDescription, alarmType, alarmInterval);
		} else {
			new DBM(context).update(alarmId, alarmTime, alarmDescription, alarmType, alarmInterval);
		}
		startAlarms(context);
	}
	
	public static void deleteAlarm(Context context, long alarmId) {
		new DBM(context).removeById(alarmId);
		Intent intent = new Intent(context.getApplicationContext(), AlarmReceiver.class);
		intent.putExtra("alarmId", alarmId);
		PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_ONE_SHOT);
		((AlarmManager)context.getSystemService(Context.ALARM_SERVICE)).cancel(pendingIntent);
		clearNotification(context);
		startAlarms(context);
	}
	
	public static void startAlarms(Context context) {
		List<Alarm> alarmList = new DBM(context).queryAlarms();
		AlarmManager alarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
		for (Alarm alarm : alarmList) {
			long alarmId = alarm.getID();
			long alarmTime = alarm.getAlarmTime();
			AlarmType alarmType = alarm.getRepeatType();
			long alarmInterval = alarm.getRepeatTime();
			
			Intent intent = new Intent(context.getApplicationContext(), AlarmReceiver.class);
			intent.putExtra("alarmId", alarmId);
			PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
			alarmManager.cancel(pendingIntent);
			while (alarmTime < System.currentTimeMillis()) {
				alarmTime += (alarmType == AlarmType.TYPE_SETTED_TIME) ? alarmInterval : 24 * 60 * 60 * 1000;
			}
			
			if (alarmType == AlarmType.TYPE_IN_24_HOUR) {
				alarmManager.set(AlarmManager.RTC_WAKEUP, alarmTime, pendingIntent);
			} else if (alarmType == AlarmType.TYPE_WEEKDAY) {
				Calendar calendar = Calendar.getInstance();
				alarmInterval = 7 * 24 * 60 * 60 * 1000;
				for (int i = 0; i < 7; i++) {
					long newAlarmTime = alarmTime + i * 24 * 60 * 60 * 1000;
					calendar.setTimeInMillis(alarmTime);
					if (calendar.get(Calendar.DAY_OF_WEEK) != Calendar.SATURDAY && calendar.get(Calendar.DAY_OF_WEEK) != Calendar.SUNDAY) {
						alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, newAlarmTime, alarmInterval, pendingIntent);
					}
				}
			} else {
				switch (alarmType) {
				case TYPE_EVERY_DAY:
					alarmInterval = 24 * 60 * 60 * 1000;
					break;
				case TYPE_EVERY_WEEK:
					alarmInterval = 7 * 24 * 60 * 60 * 1000;
					break;
				case TYPE_EVERY_MONTH:
					alarmInterval = 30 * 24 * 60 * 60 * 1000;
					break;
				case TYPE_SETTED_TIME:
					break;
				default:
					break;
				}
				alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, alarmTime, alarmInterval, pendingIntent);
			}
			setNotification(context);
		}
	}
	
	public static void showMessage(Context context, String message) {
		Toast.makeText(context, message, 1000).show();
	}
	
	public static void setNotification(Context context) {
		Intent intent = new Intent(context, AlarmListActivity.class);
		intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
		PendingIntent pIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
		
		NotificationManager nManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
		Notification notification = new Notification(R.drawable.icon, "", System.currentTimeMillis());
		notification.flags |= Notification.FLAG_NO_CLEAR;
		notification.setLatestEventInfo(context, "", "", pIntent);
		nManager.notify(Constants.NOTIFICATION_ALARM, notification);
	}
	
	public static void clearNotification(Context context) {
		NotificationManager nManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
		nManager.cancel(Constants.NOTIFICATION_ALARM);
	}
}
